package com.excel2sql.chenjun.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;

import com.excel2sql.chenjun.fileparse.ImportExcel;
import com.google.common.collect.Lists;

public class MainWindow extends MyJframe
{
	private Panel panelContainer;
	private JButton button;
	private GridLayout gridLayout = new GridLayout(3, 1);
	private JLabel jLabel;
	private CustomFileChooser customFileChooser;
	private File excelFile;
	private List<String> headList;
	private List<List<String>> datas = Lists.newArrayList();

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -4789308972242237456L;

	// 用于初始化控件
	public void initWindow()
	{
		super.setTitle("sql生成工具");
		super.setBounds(280, 160, 400, 300);
		// new出各种可视化控件
		panelContainer = new Panel();
		button = new JButton("选择Excel文件");
		jLabel = new JLabel("选择一个Excel文件，自动生成SQL", JLabel.CENTER);
		customFileChooser = new CustomFileChooser();
		FileNameExtensionFilter filter = new FileNameExtensionFilter("xls和xlsx文件", "xls", "xlsx");
		customFileChooser.setFileFilter(filter);
	}

	private void loadExcelFileData()
	{
		// parseExcel();
	}

	// 用于设置各种显示的style
	public void setViewStyle()
	{
		panelContainer.setLayout(gridLayout);
		this.add(panelContainer);

		panelContainer.setBackground(Color.WHITE);
		panelContainer.add(new Panel().add(jLabel));
		panelContainer.add(button);
		panelContainer.add(customFileChooser);
		customFileChooser.setVisible(false);
		// TODO

		this.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				super.windowClosing(e);
				System.exit(0);
			}
		});

		button.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				// TODO Auto-generated method stub
				customFileChooser.setVisible(true);
				int returnVal = customFileChooser.showOpenDialog(null);
				if (returnVal == JFileChooser.APPROVE_OPTION)
				{
					// 点击了打开按钮
					String filePath = customFileChooser.getSelectedFile().getAbsolutePath();
					System.out.println(filePath);
					try
					{
						parseExcel(filePath);
					}
					catch (InvalidFormatException e1)
					{
						e1.printStackTrace();
					}
					catch (IOException e1)
					{
						e1.printStackTrace();
					}
				}
			}
		});

		this.setVisible(true);
	}

	@SuppressWarnings("unused")
	private void parseExcel(String filePath) throws InvalidFormatException, IOException
	{
		ImportExcel ei = new ImportExcel(filePath, 0, 0);
		headList = ei.getHeadList();
		for (int i = ei.getDataRowNum(); i <= ei.getLastDataRowNum(); i++)
		{
			List<String> rows = Lists.newArrayList();
			Row row = ei.getRow(i);
			for (int j = 0; j < ei.getLastCellNum(); j++)
			{
				Object val = ei.getCellValue(row, j);
				rows.add(val.toString());
				System.out.print(val + " ");
			}
			System.out.print("\n");
			datas.add(rows);
		}
		ControlWindow controlWindow = new ControlWindow();
		controlWindow.setHeadList(headList);
		controlWindow.setDatas(datas);
		controlWindow.setVisible(true);
		controlWindow.showWindow();
		this.setVisible(false);
	}

	public File getExcelFile()
	{
		return excelFile;
	}

	public void setExcelFile(File excelFile)
	{
		this.excelFile = excelFile;
	}
	
	public List<String> getHeadList()
	{
		return headList;
	}

	public void setHeadList(List<String> headList)
	{
		this.headList = headList;
	}

	public List<List<String>> getDatas()
	{
		return datas;
	}

	public void setDatas(List<List<String>> datas)
	{
		this.datas = datas;
	}

	// 主程序入口
	public static void main(String[] args)
	{
		MainWindow mainWindow = new MainWindow();
		mainWindow.initWindow();
		mainWindow.setViewStyle();
	}

}
